<?php

/**
 * Project controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('CakeEmail', 'Network/Email');
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class BusinessesController extends AppController {

	public $components = array(
		'Cookie'
	);

	/**
	 * Controller name
	 *
	 * @var string
	 */
	public $name = 'Businesses';

	/**
	 * This controller does not use a model
	 *
	 * @var array
	 */
	public $uses = array();

	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow(array());
	}

	public function admin_index() {
		if (!empty($this->data)) {
			switch ($this->data['Paginate']['task']) {
				case 'add':
					$this->Dashboard->redirect(array('action' => 'add'));
			}
		}
		$businesses = $this->Business->find('all', array('recursive' => -1,'order'=>array('id'=>'desc'), 'conditions' => array(
				'not' => array('name' => 'admin')
		)));
		$this->set(compact('businesses'));
	}
	public function admin_add() {
		if ($this->request->is('post')) {
            $this->Business->create();
			if(!empty($this->data['Business']['picture']['name'])){
				$file = $this->data['Business']['picture'];
				move_uploaded_file($file['tmp_name'], WWW_ROOT . 'files/business/' . $file['name']);
				$this->request->data['Business']['picture'] = '250x150_'.$file['name'];
				App::import('Component', 'Image');
				$MyImageCom = new ImageComponent();                 
				$MyImageCom->prepare("files/business/".$file['name']);
				$MyImageCom->resize(250,150);//width,height,Red,Green,Blue
				$MyImageCom->save("files/business/thumbs/".'250x150_'.$file['name']);
				/*
				$MyImageCom->prepare("files/business/".$file['name']);
				$MyImageCom->resize(92,92);//width,height,Red,Green,Blue
				$MyImageCom->save("files/business/thumbs/".'92_M.92'.$file['name']);
				
				$MyImageCom->prepare("files/business/".$file['name']);
				$MyImageCom->resize(103,103);//width,height,Red,Green,Blue
				$MyImageCom->save("files/business/thumbs/".'103_S.103'.$file['name']);
				*/
			}

            if ($this->Business->save($this->request->data)) {
                $this->Session->setFlash(__('Your business has been saved.'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to add your business.'));
        }
		}

	public function admin_edit($id = null) {
		   $this->Business->id = $id; 
        if (!$this->Business->exists()) { 
            throw new NotFoundException(__('Invalid user')); 
        } 
        $this->set('business', $this->Business->read(null, $id)); 
        if ($this->request->is('post') || $this->request->is('put')) { 
			if(!empty($this->data['Business']['picture']['name'])){
				$file = $this->data['Business']['picture'];
				move_uploaded_file($file['tmp_name'], WWW_ROOT . 'files/business/' . $file['name']);
				$this->request->data['Business']['picture'] = '250x150_'.$file['name'];
				App::import('Component', 'Image');
				$MyImageCom = new ImageComponent();                 
				$MyImageCom->prepare("files/business/".$file['name']);
				$MyImageCom->resize(250,150);//width,height,Red,Green,Blue
				$MyImageCom->save("files/business/thumbs/".'250x150_'.$file['name']);
			}else{
				$this->request->data['Business']['picture'] = $this->request->data['No']['image'];
			}
            if ($this->Business->save($this->request->data)) { 
                $this->Session->setFlash(__('The business has been saved.')); 
                return $this->redirect(array('action' => 'index'));
            } else { 
                $this->Session->setFlash(__('The business could not be saved. Please, try again.')); 
            } 
        } else { 
            $this->request->data = $this->Business->read(null, $id); 
        } 
	}
	
	public function admin_delete($id = null) {
		if ($this->Business->delete($id)) {
			$this->setFlash(__('The business has been deleted'), 'success');
		} else {
			$this->setFlash(__('The business count not be deleted'));
		}
		$this->Dashboard->redirect(array('action' => 'index'), true, true);
	}
}
